require "ActionUtil"
require "MainScene"
require "Hero"

WarScene={}
local NodeID = {HeadBk=1001, UpCost=1002}
local LabelState = {LabelScaled=1, LabelFaded=2}
local HeroState = {HeroHurt=1, HeroDead=2}

WarScene.hero = "hero70"
WarScene.attackHeroName = "hero58"
WarScene.defendHeroName = "hero70"

WarScene.getNode = function()
  --if(WarScene.node) then
  --  print("WarScene.getNode return", WarScene.node)
  --  return WarScene.node
  --end
  WarScene.node = CCBReader:load("WarSceneHD.ccbi")
  WarScene.init() 
  print("WarScene.getNode", WarScene.node)
  --WarScene.node:retain()
  return WarScene.node
end

WarScene.getScene = function()
  local sc = CCScene:create()
  sc:addChild(WarScene.getNode())  
  print("WarScene.getScene", sc)
  return sc
end

WarScene.init = function()
  Hero.create(WarScene.node, "hero58", ccp(200,300))
  Hero.create(WarScene.node, WarScene.defendHeroName, ccp(600,300))

  NodeManager:getObject(WarScene.defendHeroName).normalView:setFlipX(true)
end

function onAction(node)
  local attackHero = NodeManager:getObject(WarScene.attackHeroName)
  local defendHero = NodeManager:getObject(WarScene.defendHeroName)
  --print("onAction ", node)
  local name = NodeManager:getName(node)
  print("onAction ", name)
  if node==attackHero.normalView then
    CCSpriteFrameCache:sharedSpriteFrameCache():addSpriteFramesWithFile("skilleffect8.plist")
    local ball = CCSprite:createWithSpriteFrameName("skilleffect8_1.png")
    ball:setPosition(node:getPosition())
    ball:setScale(0.5)
    node:getParent():addChild(ball)
    NodeManager:addObject("ball", ball)
    local moveby = CCMoveBy:create(0.5, ccp(400,0))
    ball:runAction(ActionUtil.createSeqAction({moveby, CCCallFuncN:create(onAction)}))
    local action = ActionUtil.createAction("skilleffect8.plist", 0.5)
    ball:runAction(action)

    local effectPath = CCFileUtils:sharedFileUtils():fullPathFromRelativePath("soundKnife.mp3")
    effectID = SimpleAudioEngine:sharedEngine():playEffect(effectPath)
  elseif node==defendHero.normalView then
    if defendHero.data.currentHP <=0 then
      --defendHero:release()
    end
  elseif name=="ball" then
    print("ball")
    node:getParent():removeChild(node, true)
    NodeManager:removeObject(node)

    CCSpriteFrameCache:sharedSpriteFrameCache():addSpriteFramesWithFile("buffeffect3.plist")
    local buffSprite = CCSprite:createWithSpriteFrameName("buffeffect3_1.png")
    local action = ActionUtil.createAction("buffeffect3.plist", 0.3)
    local ac = ActionUtil.createSeqAction({action,CCCallFuncN:create(onAction)})
    buffSprite:setPosition(defendHero.normalView:getPosition())
    defendHero.normalView:getParent():addChild(buffSprite)
    NodeManager:addObject("buff", buffSprite)
    buffSprite:runAction(ac)

    local ac = ActionUtil.createSeqAction({CCRotateBy:create(0.1, 15), CCCallFuncN:create(onAction)})
    defendHero.normalView:runAction(ac)

    local effectPath = CCFileUtils:sharedFileUtils():fullPathFromRelativePath("soundBeAttack.mp3")
    effectID = SimpleAudioEngine:sharedEngine():playEffect(effectPath)
  elseif name=="buff" then
    print("buff")
    node:getParent():removeChild(node, true)
    NodeManager:removeObject(node)
    local ac = ActionUtil.createSeqAction({CCRotateBy:create(0.1, -15), CCCallFuncN:create(onAction)})
    defendHero.normalView:runAction(ac)
    
    local label = CCLabelTTF:create("-123", "Arial", 32)
    NodeManager:addObject("number", label)
    label:setColor(ccc3(255,0,0))
    local px, py = defendHero.normalView:getPosition()
    label:setPosition(ccp(px, py+20))     
    defendHero.normalView:getParent():addChild(label)    
    local mac = CCMoveBy:create(0.3, ccp(0, 60))
    local sac = CCScaleBy:create(0.3, 1.5)
    --local fac = CCFadeOut:create(0.8)  
    label:runAction(mac)
    label:runAction(ActionUtil.createSeqAction({sac, CCCallFuncN:create(onAction)}))
    label:setTag(LabelState.LabelScaled)
    
    defendHero.data.currentHP = defendHero.data.currentHP-attackHero.data.attack
    if defendHero.data.currentHP < 0 then
      defendHero.data.currentHP = 0
      --local ac = ActionUtil.createSeqAction({CCRotateBy:create(0.1, 90), CCCallFuncN:create(onAction)})
      --defendHero.normalView:runAction(ac)
    
      local action = ActionUtil.createAction(string.format("%s.plist", WarScene.defendHeroName), 0.5)
      local ca = CCCallFuncN:create(onAction)
      local ac = ActionUtil.createSeqAction({action,ca})
      defendHero.normalView:runAction(ac)

    end
    defendHero.hpView:setPercentage(defendHero.data.currentHP/defendHero.data.totalHP*100)
  elseif name=="number" then
    print("number")
    local tag = node:getTag()
    if tag==LabelState.LabelScaled then
      node:runAction(ActionUtil.createSeqAction({CCDelayTime:create(0.2), CCFadeOut:create(0.2), CCCallFuncN:create(onAction)})) 
      node:setTag(LabelState.LabelFaded)
    elseif tag==LabelState.LabelFaded then
      node:getParent():removeChild(node, true)
    end 
  end
  
end

function onAttack(node)
  local action = ActionUtil.createAction(string.format("%s.plist", WarScene.attackHeroName), 0.5)
  local ca = CCCallFuncN:create(onAction)
  local ac = ActionUtil.createSeqAction({action,ca})

  NodeManager:getObject(WarScene.attackHeroName).normalView:runAction(ac)
  print("onJunQi ", sp)
end

function closeWarScene(node)
  CCDirector:sharedDirector():replaceScene(CCTransitionSlideInR:create(1,MainScene.getScene()))

  --local t = MainScene.node:getChildByTag(1000)
  --CCDirector:sharedDirector():popScene()
  --print("closeWarScene", WarScene.node, t)
  --MainScene.node:removeChild(WarScene.node, true)
  --local sc = CCDirector:sharedDirector():getRunningScene()
  --sc:removeChild(WarScene.node, true)
end
